{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21a7fbad",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.函数的定义： def  \n",
    "> 2.函数名的定义规则 -->变量名一样的规则  \n",
    "> 3.调用函数的方式  \n",
    "> 4.参数：  \n",
    ">> 1.形式参数（定义函数）  \n",
    ">> 2.实际参数（调用函数）  \n",
    ">> 3.有多个参数时：  \n",
    ">>> 1.位置参数（调用函数时）——>关键字参数均要放在末尾（多个不考虑顺序）  \n",
    ">>> 2.默认参数（定义函数时）——>默认参数均放在末尾（多个不考虑顺序）   \n",
    "# 本周知识  \n",
    "> 1. 函数的返回值  \n",
    "> 2.函数的参数模型  \n",
    "> 3.模块与函数的关系  \n",
    "> 4.定义一个模块，然后调用模块  \n",
    ">> A.调用模块  \n",
    ">> B.调用模块中的函数  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ec757eb",
   "metadata": {},
   "source": [
    "## 1.函数的返回值   \n",
    "\n",
    "> 1.返回值的关键字：return  \n",
    "> 2.在其他的编程语言中，一般return一定要写，不写会报错；但是在python中，return可以不写，return默认为 None  \n",
    ">> 1. 编程语言中的函数，一定与返回值  \n",
    ">> 2. 返回值的结果是一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ac2f5ce3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ll\n",
    "\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # 函数的返回结果为 full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "71a09bbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "02d3a84f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "musician = get_formatted_name(\"Li\",\"Hua\")\n",
    "print(musician)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d3252eb",
   "metadata": {},
   "source": [
    "## 2.函数的参数   \n",
    "\n",
    "### 1.函数输入的参数  \n",
    "> 1. 函数式可以输入复杂的参数的：  \n",
    ">> 1. 函数参数为列表  \n",
    ">> 2. 函数参数为字典 \n",
    "> 2. 传递任意数量的实参  \n",
    ">> 1.*args  \n",
    ">> 2.*kwargs  \n",
    " \n",
    "### 2. 函数的返回结果   \n",
    "> 1. 函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构。  \n",
    ">> 1.函数返回一个集合  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "446a5982",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ba727d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Ziqi', 'Xiaofeng']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Ziqi\",\"Xiaofeng\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97cf8c26",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "132b7c34",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d0192945",
   "metadata": {},
   "outputs": [],
   "source": [
    "# http://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad737f-7f5950b167a70a144d4c082b\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.236\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ff79271e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad737f-7f5950b167a70a144d4c082b'}\n"
     ]
    }
   ],
   "source": [
    "# di\n",
    "dict_test(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9c66ab4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://httpbin.org/get\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get[\"url\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c66afc7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get[\"args\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "173016b8",
   "metadata": {},
   "source": [
    "> 2. 传递任意数量的实参  \n",
    " \n",
    ">> 1.*args  \n",
    ">>> 1. args 可以接受多个数据（单值） \n",
    ">>> 2. args将多个数据存在一个元组当中，实际还是一个变量  \n",
    "\n",
    ">> 2.*kwargs   \n",
    ">>> 1.kwargs 可以接受多个数据(键值对)==> 关键字参数  \n",
    ">>> 2.kwargs 将多个 键值对 的数据存放在一个字典中，实际上还是一个变量  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f30d6e3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('mushroom', 'green peppers', 'extra cheese')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 回顾元组的定义\n",
    "toppings = \"mushroom\",\"green peppers\",\"extra cheese\"\n",
    "toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "368510c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args 实践：\n",
    "def make_food(food,*toppings):  \n",
    "    \"\"\"打印顾客点的所有配料\"\"\"\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "934dca7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_food = make_food(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fa14a570",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "build_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cab4fdb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs 实践\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人的信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e0b96841",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_person = person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\",e_mail=\"123456@qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7e4421c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Lihua', {'age': 18, 'job': 'student', 'address': '广州南方学院', 'e_mail': '123456@qq.com'})\n"
     ]
    }
   ],
   "source": [
    "print(build_person)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39fd4b72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "73b8912d",
   "metadata": {},
   "source": [
    "### 2. 函数的返回结果   \n",
    "> 1. 函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构。  \n",
    ">> 1.函数返回一个集合  \n",
    ">> 2.函数返回一个字典  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "34fca6ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个集合\n",
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "604a18bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'i', 'e', 'o', 'a'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels('hello Zhichao')\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2dab87b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个字典\n",
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "859eb0ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Lihua', 'age': 18, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person(\"Lihua\",18,\"student\")\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b62bc332",
   "metadata": {},
   "source": [
    "## 3. 模块  \n",
    "> 1.模块用户都不需要看底层代码  \n",
    "> 2.调用模块的方法  \n",
    ">> 1. 必须先调用模块，才可以使用  \n",
    ">>> 1. import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用方法为：moudle_name.func_name()    \n",
    ">>> 2.from moudle_name import func_name,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为 func.name()  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c9389a00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import study\n",
    "study.search4vowels('hello zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bbde8178",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：抓取某个校园新闻的所有文本链接  -------  以广州南方学院为例  \n",
    "# url = \"https://www.nfu.edu.cn/\"\n",
    "# def news_链接(url,新闻分类,page):\n",
    "#    \"\"\"抓取学校新闻的所有链接\"\"\"\n",
    " #   news_链接 = []\n",
    "  #  for i in range:(1,page)\n",
    "   #     if i == 1:\n",
    "    #        index_链接 = url+新闻分类+'/index.html' \n",
    "     #       news_链接.append(index_链接)\n",
    "      #  else:\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2054f27b",
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'news_链接' from 'study' (C:\\Users\\asus\\study\\python\\week15\\study.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-6746447e7ce9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mstudy\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnews_链接\u001b[0m \u001b[1;31m#该方法不需要再写模块的名字+.func_name()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'news_链接' from 'study' (C:\\Users\\asus\\study\\python\\week15\\study.py)"
     ]
    }
   ],
   "source": [
    "from study import news_链接 #该方法不需要再写模块的名字+.func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "43b1f1ce",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'news_链接' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-ada17197bdb1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xxyw'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'98'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'news_链接' is not defined"
     ]
    }
   ],
   "source": [
    "news_链接('https://www.nfu.edu.cn/','xxyw','98')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "49d86479",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'news_链接'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-12870192a1a2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 使用者只需要提供： url链接， 学校分类的简称， 页码\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xxyw'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'98'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'news_链接'"
     ]
    }
   ],
   "source": [
    "# 使用者只需要提供： url链接， 学校分类的简称， 页码\n",
    "result = study.news_链接('https://www.nfu.edu.cn/','xxyw','98')\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "225463c6",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'news_链接' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-5e14839f4233>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0m校园动态_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xydt'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'96'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m校园动态_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'news_链接' is not defined"
     ]
    }
   ],
   "source": [
    "校园动态_result = news_链接('https://www.nfu.edu.cn/','xydt','96')\n",
    "print(校园动态_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d92c2501",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
